#include "Router.h"

Router::Router(const TileMap &map, const SDL_Point &idxOrigin)
{
    size_t width_map = map[0].size();
    size_t height_map = map.size();
    SDL_Point idx_cur = idxOrigin;

    while (true) {
        if (idx_cur.x >= width_map || idx_cur.y >= height_map)
            break;

        if (CheckDuplicateIdx(idx_cur))
            break;
        else
            idxList.push_back(idx_cur);

        bool is_next_dir_exist = true;
        const Tile &tile = map[idx_cur.y][idx_cur.x];

        if (tile.specialFlag == 0)
            break;
        switch (tile.direction) {
            case Tile::Direction::Up:
                idx_cur.y--;
                break;
            case Tile::Direction::Down:
                idx_cur.y++;
                break;
            case Tile::Direction::Left:
                idx_cur.x--;
                break;
            case Tile::Direction::Right:
                idx_cur.x++;
                break;
            default:
                is_next_dir_exist = false;
                break;
        }

        if (!is_next_dir_exist)
            break;
    }
}

bool Router::CheckDuplicateIdx(const SDL_Point &targetIdx)
{
    for (const SDL_Point &idx: idxList) {
        if (idx.x == targetIdx.x && idx.y == targetIdx.y)
            return true;
    }
    return false;
}

const Router::IdxList &Router::GetIdxList() const
{
    return idxList;
}